package com.example.schoolblog.blog.Controller;

import com.example.schoolblog.blog.Dto.CommentDto;
import com.example.schoolblog.blog.Dto.QuestionDTO;
import com.example.schoolblog.blog.Enums.CommentTypeEnum;
import com.example.schoolblog.blog.Service.CommentServer;
import com.example.schoolblog.blog.Service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private CommentServer commentServer;

    //点击问题时的详细页
    @GetMapping("/question/{id}")
    public String question(@PathVariable(name ="id") Long id,
                           Model model){
        //根据id查询问题
        QuestionDTO questionDTO = questionService.getById(id);
        //查询该问题的评论回复
        List<CommentDto> commentDtos = commentServer.listByTargetId(id,CommentTypeEnum.QUESTION);
        //查询该问题的相关问题
        List<QuestionDTO> relatedQuestions = questionService.selectRelated(questionDTO);
        //浏览量加1
        questionService.incView(id);
        //将查询到的问题封装进model
        model.addAttribute("relatedQuestions",relatedQuestions);
        model.addAttribute("question",questionDTO);
        model.addAttribute("comments",commentDtos);
        return "question";
    }


}
